package com.qxzn.app.page.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.R;
import com.qxzn.app.page.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.DisplayUtil;
import com.qxzn.app.util.MathUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 通用EditText文本输入框
 */
public class CommonEditTextActivity extends BaseActivity {

    @BindView(R2.id.editText)
    public EditText editText;
    @BindView(R2.id.iv_clear)
    public ImageView ivClear;
    @BindView(R2.id.tv_notice)
    public TextView tvNotice;
    @BindView(R2.id.tv_bottom)
    public TextView tvBottom;
    @BindView(R2.id.iv_pwd_show_or_hint)
    public ImageView ivPwdShowOrHint;

    @Override
    protected int initLayout() {
        return R.layout.activity_common_edittext;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        init();
        showIntentData();
    }

    protected void init() {
//        EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.SPEECH_SYNTHESIS).msg(getIntent().getStringExtra("speech")).build());
        editText.setText(getIntent().getStringExtra("text"));
        AppUtil.showLog("长度：" + editText.getText().toString().length());
        editText.setSelection(editText.getText().toString().length());
        if (!TextUtils.isEmpty(getIntent().getStringExtra("hintTxtColor"))) {
            editText.setHintTextColor(Color.parseColor(getIntent().getStringExtra("hintTxtColor")));
        } else {
            editText.setHintTextColor(Color.WHITE);
        }
        setHintText(getIntent().getStringExtra("hintText"));
        setFilters();
        setMinHeight(getIntent().getIntExtra("minHeight", 0));
        setMaxHeight(getIntent().getIntExtra("maxHeight", 0));
        setInputType(getIntent().hasExtra("inputType"));
        if (getIntent().getBooleanExtra("showClear", true)) {
            ivClear.setVisibility(View.VISIBLE);
        } else {
            ivClear.setVisibility(View.GONE);
        }
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    protected void setHintText(String hintText) {
        if (!TextUtils.isEmpty(hintText)) {
            editText.setHint(hintText);
        }
    }

    protected void setInputType(boolean hasInputType) {
        if (hasInputType) {
            editText.setInputType(getIntent().getIntExtra("inputType", InputType.TYPE_NULL));
            if (getIntent().getIntExtra("inputType", InputType.TYPE_NULL) == (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD)) {
                ivPwdShowOrHint.setVisibility(View.VISIBLE);
                ivPwdShowOrHint.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (editText.getInputType() == (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD)) {
                            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                            ivPwdShowOrHint.setImageResource(R.mipmap.icon_pwd_show);
                        } else {
                            editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                            ivPwdShowOrHint.setImageResource(R.mipmap.icon_pwd_hint);
                        }
                        AppUtil.showLog("长度1：" + editText.getText().length());
                        editText.setSelection(editText.getText().length());
                    }
                });
            }
        }
    }

    private void setMaxHeight(int maxHeight) {
        if (maxHeight > 0) {
            editText.setMaxHeight(DisplayUtil.dip2px(this, maxHeight));
        }
    }

    private void setMinHeight(int minHeight) {
        if (minHeight > 0) {
            editText.setMinHeight(DisplayUtil.dip2px(this, minHeight));
        }
    }

    public void setFilters() {
        int maxLength = getIntent().getIntExtra("maxLength", 0);
        List<InputFilter> filterList = new ArrayList<>();
        if (maxLength > 0) {
            filterList.add(new InputFilter.LengthFilter(maxLength));
        }
        if (getIntent().hasExtra("maxValue")) {
            int maxValue = getIntent().getIntExtra("maxValue", 0);//注意传入的是long类型的数据
            filterList.add(new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    String newText = source.toString();
                    String oldText = dest.toString();
                    if (editText.getInputType() == (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL)) {
                        float sumText = MathUtil.parseFloat(oldText + newText);
                        if (sumText > maxValue) {
                            return dest.subSequence(dstart, dend);
                        }
                        if (".".equals(newText + "") && !(oldText + "").contains(".")) {
                            return newText;
                        } else {
                            if (MathUtil.parseFloat1(newText) == null) {
                                return "";
                            } else {
                                if ((newText + "").contains(".")) {
                                    return MathUtil.parseFloat(newText) + "";
                                } else {
                                    return MathUtil.parseInt(newText) + "";
                                }
                            }
                        }
                    } else {
                        int sumText = MathUtil.parseInt(oldText + newText);
                        if (sumText > maxValue) {
                            return dest.subSequence(dstart, dend);
                        }
                        if (MathUtil.parseInteger(newText) == null) {
                            return "";
                        } else {
                            return MathUtil.parseInt(newText) + "";
                        }
                    }
                }
            });
        }
        if (!TextUtils.isEmpty(getIntent().getStringExtra("regex"))) {//正则过滤
            String regex = getIntent().getStringExtra("regex");
            filterList.add(new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    Pattern p = Pattern.compile(regex);
                    Matcher m = p.matcher(source.toString());
                    if (!m.matches()) return "";
                    return null;
                }
            });
        }
        if (filterList.size() > 0) {
            InputFilter[] filters = new InputFilter[filterList.size()];
            for (int i = 0; i < filterList.size(); i++) {
                filters[i] = filterList.get(i);
            }
            editText.setFilters(filters);
        }
    }

    protected void showIntentData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            for (String s : bundle.keySet()) {
                AppUtil.showLog("收到的数据：" + s + "=" + bundle.get(s));
            }
        }
    }

    @OnClick(R2.id.iv_clear)
    public void onViewClicked() {
        editText.setText(null);
    }
}
